package com.spzx.user.service.impl;

import com.spzx.user.service.ISmsService;
import com.spzx.user.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: spzx-parent
 *
 * @author: 比丢比丢
 * time: 2024/7/17 19:45 周三
 * description:
 */
@Service
public class ISmsServiceImpl implements ISmsService {

    //发送短信验证码的方法
    @Override
    public Boolean sendPhoneCode(String phone, String code) {
        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";

        //你自己的AppCode
        String appcode = "d573160b510b4b45960667bd03d2f7d9";

        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        //验证码
        bodys.put("content", "code:"+code);
        //内容模板，测试模板
        bodys.put("template_id", "CST_ptdie100");  //注意，CST_ptdie100该模板ID仅为调试使用，调试结果为"status": "OK" ，即表示接口调用成功，然后联系客服报备自己的专属签名模板ID，以保证短信稳定下发
        //手机号
        bodys.put("phone_number", phone);

        try {
            /**
             * 使用HttpClient方式远程调用阿里云接口
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
